package com.paly.client.controller;

import com.paly.client.api.IUserApi;
import com.paly.client.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@RestController
@RequestMapping("/test")
public class TestController {
    @Autowired
    IUserApi userApi;

    @GetMapping("/")
    Flux<User> getAll() {
        userApi.getAll().subscribe(System.out::println);
        return userApi.getAll();
    }

    @PostMapping("/")
    Mono<User> crete(@RequestBody Mono<User> user) {
        return userApi.crete(user);
    }

    @GetMapping(value = "/getAllStream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    Flux<User> getStreamAll() {
        return userApi.getStreamAll();
    }

    @DeleteMapping("/{id}")
    Mono<Void> delete(@PathVariable("id") String id) {
        return userApi.delete(id);
    }

    @PutMapping("/{id}")
    Mono<User> update(@PathVariable("id") String id, @RequestBody Mono<User> user) {
        return userApi.update(id, user);
    }

    @GetMapping("/{id}")
    Mono<User> findById(@PathVariable("id") String id) {
        return userApi.findById(id);
    }

    @GetMapping(value = "/stream/age/{start}/{end}", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    Flux<User> getUserByAgeStream(@PathVariable("start") int start, @PathVariable("end") int end) {
        return userApi.getUserByAgeStream(start, end);
    }

    @GetMapping(value = "/age/{start}/{end}")
    Flux<User> getUserByAge(@PathVariable("start") int start, @PathVariable("end") int end) {
        return userApi.getUserByAge(start, end);
    }
}
